package com.ruoyi.web.controller.school;

import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysDept;
import com.ruoyi.common.core.domain.entity.TrainDeptAuthorizationLog;
import com.ruoyi.common.core.domain.entity.TrainPlace;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.system.service.ISysDeptService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.system.service.TrainDeptAuthorizationLogService;
import com.ruoyi.system.service.TrainPlaceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * 驾校信息
 *
 * @author ruoyi
 */
@RestController
@RequestMapping("/school/manager")
public class SchoolManagerController extends BaseController {

    @Autowired
    private ISysDeptService deptService;

    @Autowired
    private TrainPlaceService placeService;

    @Autowired
    private TrainDeptAuthorizationLogService deptAuthorizationLogService;

    /**
     * 获取驾校列表
     */
    @PreAuthorize("@ss.hasPermi('school:manager:list')")
    @GetMapping("/list")
    public TableDataInfo list(SysDept dept) {
        startPage();
        List<SysDept> depts = deptService.selectSchoolList(dept);
        return getDataTable(depts);
    }

    /**
     * 获取驾校详情
     */
    @PreAuthorize("@ss.hasPermi('school:manager:Deatil')")
    @GetMapping("/schoolDetail")
    public AjaxResult schoolDeatil(String schoolId) {
        Map map = new HashMap();
        SysDept dept = deptService.selectSchoolDeatil(schoolId);
        List<TrainPlace> place = placeService.selectPlaceBySchoolId(schoolId);
        map.put("dept",dept);
        map.put("place",place);
        return success(map);
    }
    /**
     * 增加学员授权数量
     */
    @PreAuthorize("@ss.hasPermi('school:manager:addAuthorization')")
    @GetMapping("/addImpower")
    public AjaxResult addImpower(String id,String addImpower) {
        int status = deptService.addImpower(id,addImpower);
        // 获取当前的用户
        LoginUser loginUser = SecurityUtils.getLoginUser();
        SysDept dept = deptService.selectDeptById(loginUser.getDeptId());
        //添加日志
        TrainDeptAuthorizationLog authorizationLog = new TrainDeptAuthorizationLog();
        authorizationLog.setAddUser(loginUser.getUserId().toString());
        authorizationLog.setCount(addImpower);
        authorizationLog.setSchoolId(id);
        deptAuthorizationLogService.insertLogById(authorizationLog);
        return success(status);
    }

}
